package com.wolfeyes.common.utils.network.http;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @description 获取请求响应信息工具类
 * @projectName bootdo
 * @author yanyl
 * @email 1991yanyl@163.com
 * @date 2018-08-16 15:53:58
 * @version 1.0.0
 */
public class HttpContextUtils {
	
	/**
	 * 快速获取当前请求对象request
	 * @return HttpServletRequest request
	 */
	public static HttpServletRequest getHttpServletRequest() {
		
		//方法一：
        //ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //HttpServletRequest request = attributes.getRequest();
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		//方法二：
		//HttpServletRequest request = RequestLocal.getRequest();
		
		return request;
	}
	
	/**
	 * 快速获取当前响应对象response
	 * @return HttpServletResponse response
	 */
	public static HttpServletResponse getHttpServletResponse() {
		
		//方法一：
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
		//方法二：
		//HttpServletResponse response = RequestLocal.getResponse();
		
		return response;
	}
	
	/**
	 * 快速获取当前会话session
	 * @return HttpSession session
	 */
	public static HttpSession getHttpSession() {
		
		//方法一：
        //ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //HttpServletRequest request = attributes.getRequest();
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		//方法二：
		//HttpServletRequest request = RequestLocal.getRequest();
		
		// Returns the current session associated with this request, or if the request does not have a session, creates one.
		HttpSession session = request.getSession();
		
		return session;
	}
	
}

